// 分类数据
export const categories = [
  { id: 1, name: '电子产品', slug: 'electronics' },
  { id: 2, name: '服装', slug: 'clothing' },
  { id: 3, name: '食品', slug: 'food' }
]

// 模拟用户数据
export const users = [
    {
        id: 1,
        username: 'admin',
        password: 'admin123',
        isAdmin: true
    },
    {
        id: 2,
        username: 'user1',
        password: 'user123',
        isAdmin: false
    }
]

// 模拟登录API
export const loginUser = (username, password) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const user = users.find(u =>
                u.username === username && u.password === password
            )
            if (user) {
                resolve({ success: true, user })
            } else {
                reject(new Error('用户名或密码错误'))
            }
        }, 500)
    })
}

// 模拟注册API
export const registerUser = (userData) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const userExists = users.some(u => u.username === userData.username)
            if (userExists) {
                reject(new Error('用户名已存在'))
            } else {
                const newUser = {
                    id: users.length + 1,
                    ...userData,
                    isAdmin: false
                }
                users.push(newUser)
                resolve({ success: true, user: newUser })
            }
        }, 500)
    })
}
// 在原有代码基础上添加商品数据
export const products = [
    // 电子产品
    {
        id: 1,
        name: 'iPhone 13',
        price: 5999,
        description: '最新款苹果手机',
        image: 'https://ts2.tc.mm.bing.net/th/id/OIP-C.axNFCGBRxbgIMjI0YtGLMQHaHa?r=0&rs=1&pid=ImgDetMain',
        stock: 100,
        category: 'electronics'
    },
    {
        id: 2,
        name: 'MacBook Pro',
        price: 12999,
        description: '高性能笔记本电脑',
        image: 'https://designjiaoshi.com/wp-content/uploads/2019/09/e5a4f9e8e4f7661.jpg',
        stock: 50,
        category: 'electronics'
    },
    {
        id: 3,
        name: 'AirPods Pro',
        price: 1999,
        description: '降噪无线耳机',
        image: 'https://ts1.tc.mm.bing.net/th/id/R-C.f3b7fc7d946882c77deb4e475ada1653?rik=dlc6WphZUplNbg&riu=http%3a%2f%2ffiles.nowre.com%2farticles%2f2019%2f10%2fMWP22_AV2.jpeg&ehk=BBrzy49baI3fxrqzWDHivtAoV5TJgXDzA0Mx7%2bpUw7Y%3d&risl=&pid=ImgRaw&r=0',
        stock: 200,
        category: 'electronics'
    },

    // 服装类
    {
        id: 4,
        name: '纯棉T恤',
        price: 99,
        description: '100%纯棉舒适T恤',
        image: 'https://cbu01.alicdn.com/img/ibank/O1CN01JSVcH01P7FC1fOqZJ_!!2217613491793-0-cib.220x220.jpg?_=2020',
        stock: 300,
        category: 'clothing'
    },
    {
        id: 5,
        name: '牛仔裤',
        price: 199,
        description: '经典修身牛仔裤',
        image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.q4IYJHcd3UZzGlwWTsx_egHaE8?w=278&h=211&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
        stock: 150,
        category: 'clothing'
    },

    // 食品类
    {
        id: 6,
        name: '进口巧克力',
        price: 59,
        description: '比利时进口巧克力',
        image: 'https://img.redocn.com/sheji/20240508/jinkouqiaokelilihemeishi_13312291.jpg',
        stock: 200,
        category: 'food'
    },
    {
        id: 7,
        name: '有机苹果',
        price: 15,
        description: '新鲜有机苹果',
        image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.85iXdlogit1tlQEu0ntT8QHaLG?w=147&h=211&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
        stock: 500,
        category: 'food'
    },

    // 图书类
    {
        id: 8,
        name: 'JavaScript高级编程',
        price: 89,
        description: '前端开发必备书籍',
        image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.9kgKt9N0N3ruWa-vBS9rSwHaKr?w=160&h=211&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
        stock: 80,
        category: 'books'
    },

    // 运动类
    {
        id: 9,
        name: '跑步鞋',
        price: 399,
        description: '轻便透气跑步鞋',
        image: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.z_zwgsLbc1zzG3ve3W3qXwHaFM?w=248&h=211&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2',
        stock: 120,
        category: 'sports'
    }
]

// 模拟获取商品API
export const fetchProducts = () => {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(products)
        }, 500)
    })
}

// 模拟获取单个商品API
export const fetchProductById = (id) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const product = products.find(p => p.id === Number(id))
            if (product) {
                resolve(product)
            } else {
                reject(new Error('商品不存在'))
            }
        }, 500)
    })
}
// 在原有代码基础上添加订单数据和方法
export const orders = []

// 模拟创建订单API
export const createOrder = (orderData) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            const newOrder = {
                id: orders.length + 1,
                ...orderData,
                createdAt: new Date().toISOString(),
                status: 'completed'
            }
            orders.push(newOrder)
            resolve(newOrder)
        }, 500)
    })
}

// 模拟获取用户订单API
export const fetchUserOrders = (userId) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            const userOrders = orders.filter(order => order.userId === String(userId))
            resolve(userOrders)
        }, 500)
    })
}
// 在原有代码基础上添加管理员功能相关方法
export const fetchAllOrders = () => {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(orders)
        }, 500)
    })
}

export const updateOrder = (orderId, updates) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const orderIndex = orders.findIndex(o => o.id === orderId)
            if (orderIndex !== -1) {
                orders[orderIndex] = { ...orders[orderIndex], ...updates }
                resolve(orders[orderIndex])
            } else {
                reject(new Error('订单不存在'))
            }
        }, 500)
    })
}

export const addProduct = (product) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (!product.category) {
                reject(new Error('产品必须指定分类'))
                return
            }
            const newProduct = {
                id: products.length + 1,
                ...product,
                price: Number(product.price),
                stock: Number(product.stock),
                category: product.category
            }
            products.push(newProduct)
            resolve(newProduct)
        }, 500)
    })
}

export const updateProduct = (product) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const index = products.findIndex(p => p.id === product.id)
            if (index !== -1) {
                products[index] = {
                    ...products[index],
                    ...product,
                    category: product.category || products[index].category
                }
                resolve(products[index])
            } else {
                reject(new Error('商品不存在'))
            }
        }, 500)
    })
}

export const deleteProduct = (productId) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const index = products.findIndex(p => p.id === productId)
            if (index !== -1) {
                products.splice(index, 1)
                resolve(true)
            } else {
                reject(new Error('商品不存在'))
            }
        }, 500)
    })
}

// 地址相关模拟数据和方法
export const userAddresses = [
    {
        id: 1,
        userId: 1,
        name: '张三',
        phone: '13800138000',
        province: '广东省',
        city: '深圳市',
        district: '南山区',
        detail: '科技园路1号',
        isDefault: true
    },
    {
        id: 2,
        userId: 1,
        name: '张三',
        phone: '13800138001',
        province: '北京市',
        city: '朝阳区',
        district: '',
        detail: '建国路88号',
        isDefault: false
    }
]

// 获取用户地址列表
export const fetchUserAddresses = (userId) => {
    return new Promise(resolve => {
        setTimeout(() => {
            const addresses = userAddresses.filter(addr => addr.userId === userId)
            resolve(addresses)
        }, 500)
    })
}

// 创建新地址
export const createAddress = (address) => {
    return new Promise(resolve => {
        setTimeout(() => {
            const newAddress = {
                id: userAddresses.length + 1,
                ...address,
                userId: 1 // 模拟当前用户ID
            }
            userAddresses.push(newAddress)
            resolve(newAddress)
        }, 500)
    })
}

// 更新地址
export const updateAddress = (address) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const index = userAddresses.findIndex(a => a.id === address.id)
            if (index !== -1) {
                userAddresses[index] = { ...userAddresses[index], ...address }
                resolve(userAddresses[index])
            } else {
                reject(new Error('地址不存在'))
            }
        }, 500)
    })
}

// 删除地址
export const deleteAddress = (addressId) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            const index = userAddresses.findIndex(a => a.id === addressId)
            if (index !== -1) {
                userAddresses.splice(index, 1)
                resolve(true)
            } else {
                reject(new Error('地址不存在'))
            }
        }, 500)
    })
}
